﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PayloadTracker
{
    public delegate void PinRequestDelegate(Flight f);
    public partial class FlightBubble : UserControl
    {
        public event PinRequestDelegate PinRequest;
        private Flight m_Flight;
        private Coordinate m_Pos;
        private DateTime m_LastTime;
        private System.Timers.Timer m_InvalidationTimer;

        private double m_MaxAltitude = double.MinValue;
        private double m_MaxSpeed = double.MinValue;

        public FlightBubble(Flight f)
        {
            m_InvalidationTimer = new System.Timers.Timer(1000);
            m_InvalidationTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_InvalidationTimer_Elapsed);
            m_InvalidationTimer.Start();
            m_LastTime = DateTime.MinValue;
            Visible = false;
            m_Flight = f;
            m_Flight.OnUpdate += new FlightUpdateDelegate(m_Flight_OnUpdate);
            
            
            
            InitializeComponent();
            lblPosition.LinkClicked += new LinkLabelLinkClickedEventHandler(lblPosition_LinkClicked);
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();

            if (f.CurrentTelemetry != null)
                m_Flight_OnUpdate(null, f.CurrentTelemetry);
        }

        void m_InvalidationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invalidate();
        }

        void lblPosition_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (m_Pos != null)
                System.Diagnostics.Process.Start("http://maps.google.com/maps?q=" + m_Pos.Lat.ToString() + "," + m_Pos.Long.ToString());
        }

        void m_Flight_OnUpdate(Flight flight, PayloadTracker.APRS.APRSBeacon beacon)
        {

            if (beacon.Altitude > m_MaxAltitude)
                m_MaxAltitude = beacon.Altitude;

            if (beacon.Speed > m_MaxSpeed)
                m_MaxSpeed = beacon.Speed;


            this.BeginInvoke((MethodInvoker)delegate()
            {
                lblFlightName.Text = m_Flight.Callsign;
                lblAltitude.Text = beacon.Altitude.ToString("N") + "ft MAX: "+m_MaxAltitude.ToString("N");
                lblCourse.Text = beacon.Course.ToString();
                m_Course.SetDirection(beacon.Course);
                lblSpeed.Text = beacon.Speed.ToString("N") + "mph MAX: "+m_MaxSpeed.ToString("N");
                lblTime.Text = beacon.Time.ToShortTimeString();
                lblPosition.Text = "LA: " + beacon.Position.Lat.ToString("F3") + " LN: " + beacon.Position.Long.ToString("F3");
                m_Pos = beacon.Position;
                m_LastTime = beacon.Time;
                
            }
            );
            

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            if (m_LastTime == DateTime.MinValue)
                if (m_Flight.CurrentTelemetry != null)
                    m_Flight_OnUpdate(null, m_Flight.CurrentTelemetry);


            var triangle = new Point[] { new Point(0,Height), new Point(10, Height - 30), new Point(0, Height - 30), new Point(0,Height) };
            e.Graphics.FillPolygon(Brushes.White, triangle);//draw fill
            e.Graphics.DrawPolygon(new Pen(Color.Black,2), triangle);//draw border

            var rect = new Rectangle(0,0, Width -1, Height - 30);

            e.Graphics.FillRectangle(Brushes.White, rect);
            e.Graphics.DrawRectangle(new Pen(Color.Black, 2), rect);

            if (m_LastTime != DateTime.MinValue)
                lblTime.Text = m_LastTime.ToShortTimeString() + " (" + (Math.Abs((int)(DateTime.UtcNow.TimeOfDay - m_LastTime.TimeOfDay).TotalSeconds)).ToString() + "s ago)";


            base.OnPaint(e);
        }
        private void lblX_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void lblPin_Click(object sender, EventArgs e)
        {
            PinRequestDelegate handler = PinRequest;
            if (handler != null)
                handler(m_Flight);

        }
    }
}
